<?php

namespace Service\Http\Middleware;

use Service\Http\Request;
use Service\Providers\TokenAuthenticateProviders;

class TokenAuthenticate implements Middleware
{
    public function handle(Request $request)
    {
        $request->middlewareObject['TokenAuthenticate']['Guest'] = 1;
        $request->middlewareObject['TokenAuthenticate']['User'] = null;
        if (!isset($_SERVER['HTTP_API_TOKEN'])) {
            return;
        }

        $token = $_SERVER['HTTP_API_TOKEN'];
        if (($payload = TokenAuthenticateProviders::getTokenPayload($token)) !== null) {
            $request->middlewareObject['TokenAuthenticate']['Guest'] = 0;
            $request->middlewareObject['TokenAuthenticate']['User'] = $payload;
        }

    }
}